/**
 * @description 主进程
 */
const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('path')

app.on('ready', () => {
  let win = new BrowserWindow({
    width: 800,
    height: 600,
    autoHideMenuBar: true,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'), // 预加载脚本
    }
  })

  ipcMain.on('file-save', (event, data) => {
    fileSave(data)
  })

  ipcMain.handle('file-read', (event) => {
    return fileRead()
  })

  // win.loadURL('https://www.cdjcc.edu.cn/')
  win.loadFile('./pages/index.html')
  console.log('应用准备完毕！')
})

/**
 * 保存文件
 */
const fs = require('fs')
const fileSave = (data) => {
  const dir = path.join(__dirname, 'temp/data.txt')
  fs.writeFileSync(dir, data)
}
/**
 * 读取文件
 */
const fileRead = () => {
  const dir = path.join(__dirname, 'temp/data.txt')
  return fs.readFileSync(dir, 'utf-8')
}
